package com.api.config;

import com.api.config.interceptors.SaTokenInterceptors;
import com.api.controller.applet.annotation.AAppletController;
import com.api.controller.manage.annotation.AManageController;
import com.sun.istack.internal.NotNull;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 项目MVC配置类
 *
 * @author 李大牛家的牛 (scafel@163.com)
 * @createDate 2025/5/20-13:29
 * @description TODO...
 **/
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加自定义拦截器
        registry = new SaTokenInterceptors().checkLoginInterceptors(registry);
        WebMvcConfigurer.super.addInterceptors(registry);
    }

    @Override
    public void configurePathMatch(@NotNull PathMatchConfigurer configurer) {
        // 用于处理请求接口前缀
        configurer.addPathPrefix("manage", c -> c.isAnnotationPresent(AManageController.class));
        configurer.addPathPrefix("applet", c -> c.isAnnotationPresent(AAppletController.class));
        WebMvcConfigurer.super.configurePathMatch(configurer);
    }

    /**
     * 跨域配置
     * 常用于处理前后端分离项目接口跨域问题
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8080")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
        WebMvcConfigurer.super.addCorsMappings(registry);
    }
}
